package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBException;

import net.sourceforge.stripes.action.FileBean;

import org.junit.Test;

import edu.hawaii.myisern.appconfig.jaxb.AppConfig;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Tests operation of the StackModel instance.
 *
 * @author Kevin English
 */
public class TestIsernManager {

  /**
   * Test the get researcher method. (Note: not all operations are tested. Left as exercise for
   * reader.)
   */
  @Test
  public void testGetResearchers() {
    IsernManager isernModel = new IsernManager();
    assertTrue("Checking instance identity", isernModel.getResearchers().size() > 0);
  }
  /**
   * Test the get organization method.
   *
   */
  @Test
  public void testGetOrganizations() {
    IsernManager isernModel = new IsernManager();
    assertTrue("Checking instance identity", isernModel.getOrganizations().size() > 0);
  }

  /**
   * Test the get collaborations method.
   *
   */
  @Test
  public void testGetCollaborations() {
    IsernManager isernModel = new IsernManager();
    assertTrue("Checking instance identity", isernModel.getCollaborations().size() > 0);
  }

  /**
   * Test loading the App Config Xml from a file.
   *
   * @throws JAXBException If there is a JAXB problem loading the xml.
   */
  @Test
  public void testLoadAppConfig() throws JAXBException {
    // IsernManager isernModel = new IsernManager();
    AppConfig appConfig = IsernManager.loadAppConfig("xml" + File.separatorChar + "examples"
        + File.separatorChar + "appconfig.xml");
    assertEquals("username is not null", "myisern", appConfig.getUsername());
    assertNotNull("password is not null", appConfig.getPassword());
    assertNotNull("Collaborations file is not null", appConfig.getCollaborationsXmlFilename());
    assertNotNull("Organizations file is not null", appConfig.getOrganizationsXmlFilename());
    assertNotNull("Researchers file is not null", appConfig.getResearchersXmlFilename());
  }

  /**
   * Test getting the App Config from the static object.
   *
   * @throws JAXBException If there is a JAXB problem loading the xml.
   */
  @Test
  public void testgetAppConfig() throws JAXBException {
    // IsernManager isernModel = new IsernManager();
    AppConfig appConfig = IsernManager.getAppConfig();
    assertEquals("username is not null", "myisern", appConfig.getUsername());
    assertNotNull("password is not null", appConfig.getPassword());
    assertNotNull("Collaborations file is not null", appConfig.getCollaborationsXmlFilename());
    assertNotNull("Organizations file is not null", appConfig.getOrganizationsXmlFilename());
    assertNotNull("Researchers file is not null", appConfig.getResearchersXmlFilename());
  }

  /**
   * Test creating a collaboration.
   *
   */
  @Test
  public void testCreateCollaboration() {
    IsernManager isernManager = new IsernManager();
    Collaboration collaboration = new Collaboration();
    collaboration.setName("My Collab 1");
    int oldSize = isernManager.getCollaborations().size();
    isernManager.createCollaboration(collaboration);
    assertEquals("New getCollaborations size should be old size plus 1", oldSize + 1, isernManager
        .getCollaborations().size());
  }

  /**
   * Test creating an invalid collaboration..
   *
   */
  @Test
  public void testCreateCollaborationFail() {
    IsernManager isernManager = new IsernManager();
    Collaboration collaboration = new Collaboration();
    int oldSize = isernManager.getCollaborations().size();
    isernManager.createCollaboration(collaboration);
    assertEquals("New getCollaborations size should be old size", oldSize, isernManager
        .getCollaborations().size());
  }

  /**
   * Test creating an organization.
   *
   */
  @Test
  public void testCreateOrganization() {
    IsernManager isernManager = new IsernManager();
    Organization organization = new Organization();
    organization.setName("My Org11 1");
    int oldSize = isernManager.getOrganizations().size();
    isernManager.createOrganization(organization);
    assertEquals("New getOrganizations size should be old size plus 1", oldSize + 1, isernManager
        .getOrganizations().size());
  }

  /**
   * Test creating an invalid organization..
   *
   */
  @Test
  public void testCreateOrganizationFail() {
    IsernManager isernManager = new IsernManager();
    Organization organization = new Organization();
    int oldSize = isernManager.getOrganizations().size();
    isernManager.createOrganization(organization);
    assertEquals("New getOrganizations size should be old size ", oldSize, isernManager
        .getOrganizations().size());
  }

  /**
   * Test creating an researcher.
   *
   */
  @Test
  public void testCreateResearcher() {
    IsernManager isernManager = new IsernManager();
    Researcher researcher = new Researcher();
    researcher.setName("My Org11 1");
    int oldSize = isernManager.getResearchers().size();
    isernManager.createResearcher(researcher);
    assertEquals("New getResearchers size should be old size plus 1", oldSize + 1, isernManager
        .getResearchers().size());
  }

  /**
   * Test creating an invalid researcher..
   *
   */
  @Test
  public void testCreateResearcherFail() {
    IsernManager isernManager = new IsernManager();
    Researcher researcher = new Researcher();
    int oldSize = isernManager.getResearchers().size();
    isernManager.createResearcher(researcher);
    assertEquals("New  getResearcherssize should be old size", oldSize, isernManager
        .getResearchers().size());
  }

  /**
   * Test updating researcher.
   *
   * @throws IOException if there is a file error.
   *
   * @throws JAXBException If there is a JAXB problem loading the xml.
   */
  @Test
  public void testUdateResearcher() throws IOException, JAXBException {
    IsernManager isernManager = new IsernManager();
    Researcher oldResearcher = isernManager.getResearchers().get(0);
    Researcher newResearcher = new Researcher();
    Id oldId = new Id(oldResearcher.getName());
    assertTrue("old Id should exist in ids", isernManager.getIds().contains(oldId));
    Id newId = new Id("New Researcher Name");
    newResearcher.setName(newId.toString());
    isernManager.updateResearcher(oldId, newResearcher);
    assertTrue("new Id should exist in ids", isernManager.getResearchers().contains(newResearcher));
    isernManager.updateResearcher(newId, oldResearcher);
    assertTrue("new Id should exist in ids", isernManager.getResearchers().contains(oldResearcher));
  }
  
  /**
   * Test importXml. 
   * Method should return current value indicates what kind of file is imported.
   */
  @Test
  public void testImportXml() {
    String path = "xml" + File.separatorChar + "examples" + File.separatorChar;
    File collaborationXml = new File(path + "collaborations.test.xml");
    File organizationXml = new File(path + "organizations.test.xml");
    File researcherXml = new File(path + "researchers.test.xml");
    FileBean importCollaboration = new FileBean(collaborationXml, null, null);
    FileBean importOrganization = new FileBean(organizationXml, null, null);
    FileBean importResearcher = new FileBean(researcherXml, null, null);
    IsernManager isernManager = new IsernManager();
    assertEquals("Import Collaboration", 1, isernManager.importXml(importCollaboration));
    assertEquals("Import Organization", 0,isernManager.importXml(importOrganization));
    assertEquals("Import Researcher", 2,isernManager.importXml(importResearcher));
  }
}
